import os
# from datetime import datetime # 原来用的注释掉
from django.utils import timezone as datetime # 不想大面积修改就这么做
from myadmin.models import Hosts
from dao.project_url import url

url = url()


def get_nodes():
    init_nodes()  # 扫描前先将数据库中所有主机置为状态0
    # 扫描状态
    os.system("kubectl get nodes | grep Ready > %s/static/hosts/ip_temp.txt" % url)
    os.system("awk '{print $2}' %s/static/hosts/ip_temp.txt > %s/static/hosts/status.txt" % (url, url))
    # 扫描ip
    os.system("kubectl describe node | grep InternalIP > %s/static/hosts/ip_temp.txt" % url)
    os.system("awk '{print $2}' %s/static/hosts/ip_temp.txt > %s/static/hosts/ip.txt" % (url, url))
    # 扫描主机名
    os.system("kubectl describe node | grep Hostname > %s/static/hosts/ip_temp.txt" % url)
    os.system("awk '{print $2}' %s/static/hosts/ip_temp.txt > %s/static/hosts/name.txt" % (url, url))
    # 主机内存cpu使用情况
    os.system("kubectl top node 2> /dev/null | grep -v NAME > %s/static/hosts/cpu_tmp.txt" % url)
    os.system("awk '{print $3}' %s/static/hosts/cpu_tmp.txt | awk -F '%s' '{print $1}' > %s/static/hosts/cpu.txt" % (url, '%', url))
    os.system("awk '{print $5}' %s/static/hosts/cpu_tmp.txt | awk -F '%s' '{print $1}' > %s/static/hosts/memory.txt" % (url, '%', url))
    with open("{}/static/hosts/ip.txt".format(url), 'r') as i, open('{}/static/hosts/status.txt'.format(url), 'r') as s, open('{}/static/hosts/name.txt'.format(url), 'r') as n, open("{}/static/hosts/cpu.txt".format(url), 'r') as c, open("{}/static/hosts/memory.txt".format(url), 'r') as m:
        ip_list = i.readlines()
        name_list = n.readlines()
        status_list = s.readlines()
        cpu_list = c.readlines()
        memory_list = m.readlines()
    i.close()
    s.close()
    n.close()
    c.close()
    m.close()

    i = 0
    for ip, status, name in zip(ip_list, status_list, name_list):
        if Hosts.objects.filter(ip=ip.rstrip()).exists():
            ob = Hosts.objects.get(ip=ip.rstrip())
            ob.name = name.rstrip()
            if status.rstrip() == 'Ready':
                ob.status = 1
                ob.cpu = cpu_list[i]
                ob.ram = memory_list[i]
                i += 1
            else:
                ob.status = 2
                ob.cpu = 0
                ob.ram = 0

            ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            ob.save()
        else:
            ob = Hosts()
            ob.ip = ip.rstrip()
            ob.name = name.rstrip()
            if status.rstrip() == 'Ready':
                ob.status = 1
                ob.cpu = cpu_list[i]
                ob.ram = memory_list[i]
                i += 1
            else:
                ob.status = 2
                ob.cpu = 0
                ob.ram = 0

            ob.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            ob.save()

        # ip_dic = {'ip': ip.rstrip()}
        # status_dic = {'status': status.rstrip()}
        # tmp_list = [ip_dic, status_dic]
        # list.append(tmp_list)
    # return list


def init_nodes():
    qs = Hosts.objects.filter(status__gt=0)
    for ob in qs:
        ob.status = 0
        ob.save()